package com.example.tangyangkai.testdemo;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.loadmore.LoadMoreView;
import com.okmin.base.R;

import java.util.ArrayList;

/**
 * Created by tangyangkai on 16/5/12.
 */
public class TabLayoutFragment extends Fragment implements TabsAdapter.onStartDragListener {
    public static final String TABLAYOUT_FRAGMENT = "tab_fragment";
    public static final int TABS = 100;
    private TextView txt, sortTxt;
    private View sortView;
    private RecyclerView mRecyclerview;
    private int type;
    private TabsAdapter adapter;
    private ItemTouchHelper mItemTouchHelper;
    private BaseQuickAdapter<String, BaseViewHolder> mAdapter;
    private int count;
    private ArrayList<String> mStrings;


    public static TabLayoutFragment newInstance(int type) {
        TabLayoutFragment fragment = new TabLayoutFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(TABLAYOUT_FRAGMENT, type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = (int) getArguments().getSerializable(TABLAYOUT_FRAGMENT);
        }

    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tablayout, container, false);
        initView(view);
        return view;
    }


    protected void initView(View view) {
        txt = (TextView) view.findViewById(R.id.tab_txt);
        txt.setText(this+"这里是第" + type + "个fragment");
        sortTxt = (TextView) view.findViewById(R.id.sort_txt);
        sortView = view.findViewById(R.id.sort_view);
        mRecyclerview = (RecyclerView) view.findViewById(R.id.sorts_recyclerview);
            sortTxt.setVisibility(View.VISIBLE);
            sortView.setVisibility(View.VISIBLE);
            txt.setVisibility(View.GONE);
            mRecyclerview.setVisibility(View.VISIBLE);
            initSortViews();
    }

    //加载排序布局
    private void initSortViews() {
        mStrings = new ArrayList<>();
        mStrings.add("yi1");
        mStrings.add("er2");
        mStrings.add("san3");
        mStrings.add("san4");
        mStrings.add("san5");
        mStrings.add("san6");
        mStrings.add("san7");
        mStrings.add("san8");
        mStrings.add("san9");
        mStrings.add("san10");
        mStrings.add("san11");
        mStrings.add("san12");
        mStrings.add("san13");
        mStrings.add("san14");
        mStrings.add("san15");
        mStrings.add("san16");
        mStrings.add("san17");
        mStrings.add("san18");
        mStrings.add("san19");
        mStrings.add("san20");
        mStrings.add("san21");
        mStrings.add("san22");
        mStrings.add("san23");
        final LinearLayoutManager manger = new LinearLayoutManager(getActivity());
        mRecyclerview.setLayoutManager(manger);
        mAdapter = new BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_recyclerview, mStrings) {
            @Override
            protected void convert(BaseViewHolder helper, String item) {
                helper.setText(R.id.recycle_txt, item).setImageResource(R.id.recycle_img, R.mipmap.ic_launcher);
            }
        };
        mRecyclerview.setAdapter(mAdapter);
       /* mAdapter.setUpFetchEnable(true);
        mAdapter.setUpFetchListener(new BaseQuickAdapter.UpFetchListener() {
            @Override
            public void onUpFetch() {
                Toast.makeText(getActivity(),"开始刷新拉",Toast.LENGTH_LONG).show();
                mAdapter.setUpFetching(true);
                *//**
                 * get data from internet.
                 *//*
                mRecyclerview.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        count++;
                        Log.e("tag","次数"+count+"大小"+mAdapter.getItemCount());
                        mAdapter.addData(0, TabLayoutActivity.tabTitle);
                        mAdapter.setUpFetching(false);
                        if (count > 5) {
                            mAdapter.setUpFetchEnable(false);
                        }
                    }
                }, 300);
            }
        });*/
        mAdapter.setLoadMoreView(new CustomLoadMoreView());
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override public void onLoadMoreRequested() {
                Log.e("tag","次数"+this);
                mRecyclerview.postDelayed(new Runnable() {
                    @Override
                    public void run() {
//                        if (mCurrentCounter >= TOTAL_COUNTER) {
//                            //数据全部加载完毕
//                            mQuickAdapter.loadMoreEnd();
//                        } else {
//                            if (isErr) {
//                                //成功获取更多数据
//                                mQuickAdapter.addData(DataServer.getSampleData(PAGE_SIZE));
//                                mCurrentCounter = mQuickAdapter.getData().size();
//                                mQuickAdapter.loadMoreComplete();
//                            } else {
//                                //获取更多数据失败
//                                isErr = true;
//                                Toast.makeText(PullToRefreshUseActivity.this, R.string.network_err, Toast.LENGTH_LONG).show();
//                                mQuickAdapter.loadMoreFail();
//
//                            }
//                        }
                        mAdapter.addData(mStrings);
                        Log.e("tag","次数"+count+"大小"+mAdapter.getItemCount());
                        Toast.makeText(getActivity(),"下拉加载拉",Toast.LENGTH_LONG).show();
                        mAdapter.loadMoreComplete();
                    }

                }, 2000);
            }
        }, mRecyclerview);
        mAdapter.disableLoadMoreIfNotFullPage();
    }

    @Override
    public void startDrag(RecyclerView.ViewHolder viewHolder) {
        mItemTouchHelper.startDrag(viewHolder);
    }
    public final class CustomLoadMoreView extends LoadMoreView {

        @Override public int getLayoutId() {
            return R.layout.view_load_more;
        }

        /**
         * 如果返回true，数据全部加载完毕后会隐藏加载更多
         * 如果返回false，数据全部加载完毕后会显示getLoadEndViewId()布局
         */
        @Override public boolean isLoadEndGone() {
            return false;
        }

        @Override protected int getLoadingViewId() {
            return R.id.load_more_loading_view;
        }

        @Override protected int getLoadFailViewId() {
            return R.id.load_more_load_fail_view;
        }

        /**
         * isLoadEndGone()为true，可以返回0
         * isLoadEndGone()为false，不能返回0
         */
        @Override protected int getLoadEndViewId() {
            return R.id.load_more_load_end_view;
        }
    }
}
